package flc.ast.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.basic.view.container.StkRelativeLayout;
import stark.common.bean.StkResBean;

public class AvatarAdapter extends StkProviderMultiAdapter<StkResBean> {

    public AvatarAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(150));
        addItemProvider(new AvatarProvider());
    }

    private GridLayoutManager gridLayoutManager;

    private boolean isMore = false;

    public void setGridLayoutManager(GridLayoutManager gridLayoutManager) {
        this.gridLayoutManager = gridLayoutManager;
    }
    public void setMore(boolean more) {
        isMore = more;
    }

    private class AvatarProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_avatar;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {
            ViewGroup.LayoutParams parm = baseViewHolder.itemView.getLayoutParams();
            parm.height = gridLayoutManager.getWidth() / gridLayoutManager.getSpanCount()
                    - 2 * baseViewHolder.itemView.getPaddingLeft() - 2 * ((ViewGroup.MarginLayoutParams) parm).leftMargin;
            Glide.with(getContext()).load(bean.getUrl())
                    .into((ImageView) baseViewHolder.getView(R.id.ivAvatarItemImg));
            if (baseViewHolder.getBindingAdapterPosition() == 8 && isMore) {
                baseViewHolder.getView(R.id.rlAvatarItemMore).setVisibility(View.VISIBLE);
            } else {
                baseViewHolder.getView(R.id.rlAvatarItemMore).setVisibility(View.GONE);
            }
        }

    }
}
